package org.okeoe.gen.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MyMapUtils
 * @Description:
 * @Author DJ
 * @Date 2019/10/31
 * @Version V1.0
 **/
public class MyMapUtils {
    /**
     * @Description: key转 数组
     * @Param: [paramsMap]
     * @return: java.util.List<java.lang.String>
     * @Author: dj
     * @Date: 2019/10/31
     */
    public static List<String> mapKeyToArray(Map<String, BigDecimal> paramsMap) {
        if (null == paramsMap) {
            return null;
        }
        List<String> list = new LinkedList<>();
        paramsMap.forEach((key, value) -> {
            list.add(key);
        });
        return list;
    }

    /**
     * map key 转字符串，（43,3434,343）
     *
     * @param map
     * @return
     */
    public static String mapKeyToStrArray(Map<String, String> map) {
        if (null == map) {
            return null;
        }
        return map.keySet().toString().replace("[", "").replace("]", "").replace(" ", "");
    }

    /**
     * @Description: 值转数组
     * @Param: [paramsMap]
     * @return: java.util.List<java.lang.Double>
     * @Author: dj
     * @Date: 2019/10/31
     */
    public static List<BigDecimal> valueToArray(Map<String, BigDecimal> paramsMap) {
        if (null == paramsMap) {
            return null;
        }
        List<BigDecimal> list = new LinkedList<>();
        paramsMap.forEach((key, value) -> {
            list.add(value);
        });
        return list;
    }
    public static List<String> nameToArray(Map<String, BigDecimal> paramsMap) {
        if (null == paramsMap) {
            return null;
        }
        List<String> list = new LinkedList<>();
        paramsMap.forEach((key, value) -> {
            list.add(key);
        });
        return list;
    }
    public static BigDecimal mapListToBigDecimal(List<Map<String, Object>> mapList) {
        if (null == mapList) {
            return new BigDecimal(0);
        }
        for (Map<String, Object> map : mapList) {
            BigDecimal fare = new BigDecimal(0);
            if(null == map){
                return fare;
            }
            //遍历map的key集合 获取对应key的value
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if ("val".equals(entry.getKey())) {
                    fare = new BigDecimal(String.valueOf(entry.getValue()));
                }
            }
            return fare;
        }
        return new BigDecimal(0);
    }


    /**
     * 查询条件
     *
     * @param map
     * @return
     */
    public static String mapToSelectWhere(Map<String, String> map) {
        if (map.size() == 0) {
            return "1=1";
        }
        StringBuilder selStr = new StringBuilder("");
        map.forEach((key, value) -> {
            selStr.append("  AND " + key + "='" + value + "'");
        });
        return selStr.toString();
    }

    public static Map<String, BigDecimal> listMapToMap(List<Map<String, Object>> mapList) {
        Map<String, BigDecimal> baseMap = new HashMap<>();
        if (null == mapList) {
            return null;
        }
        for (Map<String, Object> map : mapList) {
            String base = null;
            BigDecimal fare = null;
            if(null == map){
                continue;
            }
            //遍历map的key集合 获取对应key的value
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if ("name".equals(entry.getKey())) {
                    base = String.valueOf(entry.getValue());
                } else if ("val".equals(entry.getKey())) {
                    fare = new BigDecimal(String.valueOf(entry.getValue()));
                }
                if (StringUtils.isNotBlank(base)) {
                    baseMap.put(base, fare);
                }
            }
        }
        return baseMap;


    }

    public static Map<String, Object> listMapDictToMap(List<Map<String, Object>> mapList) {
        Map<String, Object> baseMap = new HashMap<>();
        if (null == mapList) {
            return null;
        }
        for (Map<String, Object> map : mapList) {
            String base = null;
            String fare = null;
            //遍历map的key集合 获取对应key的value
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if ("id".equals(entry.getKey())) {
                    base = String.valueOf(entry.getValue());
                } else if ("name".equals(entry.getKey())) {
                    fare = String.valueOf(entry.getValue());
                }
                if (StringUtils.isNotBlank(base)) {
                    baseMap.put(base, fare);
                }
            }
        }
        return baseMap;
    }

    /**
     * 获取map中第一个key值
     *
     * @param map 数据源
     * @return
     */
    public static String getFirstKey(Map<Object, Object> map) {
        Object obj = null;
        for (Object o : map.keySet()) {
            obj = o;
            break;
        }
        if(obj != null){
            return obj.toString();
        }
        return "";
    }
}
